package com.android.tea.shopper;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

import com.android.tea.R;
import com.android.tea.TeaEncyclopediaApp;
import com.android.tea.widght.TEAAlertDialog;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;

public class ShopperNearbyActivity extends MapActivity implements
		View.OnClickListener {
	protected static final String TAG = "PMapActivity";
	protected static final int ID_DELETE_OVERLAY = 2000;
	public static final int STATE_CREATE = 0;
	public static final int STATE_DEL = 1;
	public static final int STATE_POINT = 2;
	private BMapManager mBMapMan;
	public static MapView mMapView;
	public static View mPopView = null;
	public ImageButton ib_choeese;
	public ImageButton ib_refresh;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		TeaEncyclopediaApp.list.add(this);
		
		ib_choeese = (ImageButton) findViewById(R.id.btn_chooese);
		ib_choeese.setOnClickListener(this);
		ib_refresh = (ImageButton) findViewById(R.id.btn_refresh);
		ib_refresh.setOnClickListener(this);
		mBMapMan = new BMapManager(getApplication());
		mBMapMan.init("1F572AAE2DC844C03D5AF0C9A001026E03BD1618",
				new MKGeneralListener() {
					@Override
					public void onGetPermissionState(int iError) {
						// 返回授权验证错误，通过错误代码判断原因，MKEvent中常量值。
						Log.i(TAG, "permission error is " + iError);
					}

					@Override
					public void onGetNetworkState(int iError) {
						// 返回网络错误，通过错误代码判断原因，MKEvent中常量值。
						Log.i(TAG, "Network error is " + iError);
					}
				});
		super.initMapActivity(mBMapMan);

		mMapView = (MapView) findViewById(R.id.bmapsView);
		mMapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件
		mMapView.setDoubleClickZooming(true);
		mMapView.getOverlays().add(new GetOverlay());
		MapController mMapController = mMapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放
		mMapController.setZoom(12); // 设置地图zoom级别
		
		double[] longitudeLatitude = getLongitudeLatitude(this);

//		GeoPoint lGeoPoint = new GeoPoint((int)longitudeLatitude[0], (int)longitudeLatitude[1]);
//		mMapView.getController().animateTo(lGeoPoint);  

	}
	
	public double[] getLongitudeLatitude(Context context) {
		double[] location = { 0.0D, 0.0D };

		try {
			LocationManager lm = (LocationManager) context
					.getSystemService(Context.LOCATION_SERVICE);
			Location locationInfo = lm
					.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			if (locationInfo != null) {
				location[0] = locationInfo.getLongitude();
				location[1] = locationInfo.getLatitude();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return location;
	}

	double longitude, latitude;

	class GetOverlay extends Overlay {

		@Override
		public boolean onTap(GeoPoint geo, MapView arg1) {

			longitude = geo.getLongitudeE6() / 1E6;
			latitude = geo.getLatitudeE6() / 1E6;
			if (isChoese) {
				android.content.DialogInterface.OnClickListener onClickListener = new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {

						dialog.dismiss();
					}
				};

				new TEAAlertDialog.Builder(ShopperActivity.instance).setItems(
						null, onClickListener).setTitle("选择范围").setMessage(
						"是否在该位置周围搜索？").setNegativeButton("取消", aClickListener)
						.setPositiveButton("是", bClickListener).show();
			}
			return super.onTap(geo, arg1);
		}
	}

	android.content.DialogInterface.OnClickListener aClickListener = new android.content.DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {

			dialog.dismiss();
		}

	};

	android.content.DialogInterface.OnClickListener bClickListener = new android.content.DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {

			ShopperActivity.instance.onTabChange("Recommend");
			ShopperActivity.instance.tabHost.setCurrentTab(0);
			GoodShopperActivity.instance
					.StartSearch(longitude + "," + latitude);
			dialog.dismiss();

		}

	};

	@Override
	protected void onStart() {

		super.onStart();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	@Override
	public void onBackPressed() {
		Activity parent = this.getParent();
		if(parent!=null) {
			parent.onBackPressed();
		}else{
			super.onBackPressed();
		}
	}

	@Override
	protected void onDestroy() {
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (mBMapMan != null) {
			mBMapMan.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (mBMapMan != null) {
			mBMapMan.start();
		}
		super.onResume();
	}

	boolean isChoese = false;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			super.onBackPressed();
			break;
		case R.id.btn_chooese:

			if (!isChoese) {
				ib_choeese.setBackgroundColor(Color.GREEN);
				isChoese = true;
			} else {
				ib_choeese.setBackgroundDrawable(null);
				isChoese = false;
			}

			break;
		case R.id.btn_refresh:
			ShopperActivity.instance.onTabChange("Recommend");
			ShopperActivity.instance.tabHost.setCurrentTab(0);
			GoodShopperActivity.instance
					.StartSearch(longitude + "," + latitude);
			break;

		default:
			break;
		}

	}
}
